package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.resource.ImageService;
import com.bocloud.cmp.model.ImgBean;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    /**
     * 列表展示
     * 
     * @param page
     * @param rows
     * @param params
     * @param sorter
     * @param simple
     * @return
     */
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
            @RequestParam(value = Common.ROWS, required = false) Integer rows,
            @RequestParam(value = Common.PARAMS, required = false) String params,
            @RequestParam(value = Common.SORTER, required = false) String sorter,
            @RequestParam(value = Common.SIMPLE, required = false) Boolean simple) {
        @SuppressWarnings("unchecked")
        Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
        return imageService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);
    }

    /**
     * 获取名字
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/listName", method = { RequestMethod.POST })
    public BsmResult listName(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        Long id = object.getLong(Common.ID);
        Long region = object.getLong(Common.REGION);
        return imageService.listName(id, region);
    }

    /**
     * 获取版本
     * 
     * @param params
     * @param user
     * @return
     */
    @RequestMapping(value = "/listVersion", method = { RequestMethod.POST })
    public BsmResult listVersion(@RequestParam(value = Common.PARAMS, required = true) String params,
            @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        Long id = object.getLong(Common.ID);
        Long region = object.getLong(Common.REGION);
        String name = object.getString(Common.NAME);
        return imageService.listVersion(id, region, name);
    }

    /**
     * 详情
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
    public BsmResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return imageService.detail(id);
    }

    /**
     * 删除
     * 
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
    public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        return imageService.remove(id, user.getId());
    }

    /**
     * 修改
     * 
     * @param params
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
    public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
            @PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
        JSONObject object = JSONTools.isJSONObj(params);
        if (null != object) {
            ImgBean bean = JSONObject.parseObject(object.toJSONString(), ImgBean.class);
            return imageService.modify(bean, user.getId());
        } else {
            return ResultTools.formatErrResult();
        }
    }

}
